package com.bjpowernode.dataservice.service.impl;

import com.bjpowernode.api.model.FinanceAccount;
import com.bjpowernode.api.model.SysUser;
import com.bjpowernode.api.result.DubboResult;
import com.bjpowernode.api.service.UserService;
import com.bjpowernode.api.vo.UserAccountInfo;
import com.bjpowernode.dataservice.mapper.FinanceAccountMapper;
import com.bjpowernode.dataservice.mapper.SysUserMapper;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.http.client.utils.DateUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.UUID;

/**
 * @description:
 * @author: panDeng_Zhu
 * @time: 2022/11/7 15:21
 */
@DubboService(interfaceClass = UserService.class, version = "1.0")
public class UserServiceImpl implements UserService {

    @Value("${salt}")
    private String passWordSalt;

    @Resource
    private SysUserMapper userMapper;

    @Resource
    private FinanceAccountMapper accountMapper;

    //用户登录
    @Transactional(rollbackFor = Exception.class)
    @Override
    public SysUser userLogin(String phone, String loginPassword) {
        SysUser user = null;
        if (!StringUtils.isAnyBlank(phone, loginPassword)) {
            //给密码加盐(一个自定义的字符串，混淆密码，增加安全性)
            String newMd5Password = DigestUtils.md5Hex(loginPassword + passWordSalt);
            user = userMapper.selectByLogin(phone, newMd5Password);

            //更新最后一次登录时间
            if (user != null) {
                user.setLastLoginTime(new Date());
                userMapper.updateByPrimaryKeySelective(user);
            }
        }
        return user;
    }

    //用户注册
    @Transactional(rollbackFor = Exception.class)
    @Override
    public synchronized DubboResult register(String phone, String loginPassword) {
        DubboResult result = new DubboResult();
        //1.检查手机号是否存在
        SysUser user = userMapper.selectByPhone(phone);
        if (user == null) {
            //2.手机号未注册，可以注册新用户
            //给密码加盐(一个自定义的字符串，混淆密码，增加安全性)
            String newMd5Password = DigestUtils.md5Hex(loginPassword + passWordSalt);
            //3.添加user
            user = new SysUser();
            user.setAddTime(new Date());
            user.setPhone(phone);
            user.setLoginPassword(newMd5Password);
            int rows = userMapper.insertUser(user);

            //4.创建account账号
            FinanceAccount account = new FinanceAccount();
            account.setAvailableMoney(new BigDecimal("0"));
            account.setUid(user.getId());
            rows = accountMapper.insertSelective(account);

            //5.注册成功
            result.setInvoke(true);
            result.setCode(50000);
            result.setData(user);
            result.setMsg("注册成功");
        } else {
            //手机号已注册
            result.setCode(50001);
            result.setInvoke(false);
            result.setMsg("手机号已注册");
        }
        return result;
    }

    /**
     * 按主键查询
     *
     * @param userId
     * @return
     */
    @Override
    public SysUser queryByUserId(Integer userId) {
        SysUser user = null;
        if (userId != null && userId > 0) {
            user = userMapper.selectById(userId);
        }
        return user;
    }

    /**
     *  更新姓名和身份证号
     * @param userId
     * @param name
     * @param idCard
     * @return
     */
    @Override
    public boolean modifyRealName(Integer userId, String name, String idCard) {
        SysUser user = new SysUser();
        user.setId(userId);
        user.setName(name);
        user.setIdCard(idCard);
        int rows = userMapper.updateByPrimaryKeySelective(user);
        return rows > 0 ? true : false;
    }

    /**
     * 查询用户和资金的信息
     * @param userId
     * @return
     */
    @Override
    public UserAccountInfo queryUserAndAccountByUserId(Integer userId) {
        UserAccountInfo accountInfo = null;
        if (userId != null && userId > 0) {
            accountInfo=userMapper.selectUserAndAccountByUserId(userId);
        }
        return accountInfo;
    }
}
